/**
 * 
 */
package com.hirstlogics.boxarrow.command;

/**
 * @author steve
 *
 */
public class LineParser {
	String cmd;
	String args;
	int cmd_key;
	
	public static final int QUIT_CODE=0;
	public static final int NEWMODEL_CODE=1;
	public static final int INFO_CODE=2;
	public static final int OPEN_CODE=3;
	public static final int SAVE_CODE=4;
	public static final int SAVEAS_CODE=5;
	public static final int ADDBOX_CODE=6;
	public static final int LISTBOXES_CODE=7;
	public static final int NEWBOX_CODE=8;
	public static final int SETDESC_CODE=9;
	public static final int HELP_CODE=10;
	public static final int FIND_CODE=11;
	public static final int NEWSORT_CODE=12;
	public static final int CONNECT_CODE=13;
	public static final int LISTARROWS_CODE=14;
	public static final int POPSTACK_CODE=15;

	public static final String[] commands = {"quit","newModel", "info", 
			                   "open", "save", "saveAs", 
			                   "addBox", "listBoxes", "newBox", "setDescription",
			                   "help","find","newSort", "connect", "listArrows", "popStack"};
	
	public final int[] codes = {QUIT_CODE,NEWMODEL_CODE,INFO_CODE,
			                    OPEN_CODE,SAVE_CODE,SAVEAS_CODE,
			                    ADDBOX_CODE,LISTBOXES_CODE,NEWBOX_CODE,SETDESC_CODE,
			                    HELP_CODE, FIND_CODE, NEWSORT_CODE, CONNECT_CODE, LISTARROWS_CODE,
			                    POPSTACK_CODE};
	
	public LineParser(String line){
		String pressedLine = line.trim();
		int space = pressedLine.indexOf(" ");
		if (space==-1) space = pressedLine.length();
		cmd = pressedLine.substring(0,space);
		if (space==pressedLine.length()) args="";
		else args = pressedLine.substring(space+1);
	}
	
	public String getCmd() {
		return cmd;
	}
	
	public String getArgs() {
		return args;
	}
	
	public int getCmdCode() {
		int returnCode = -1;
		int idx = -1;
		while (++idx<codes.length && returnCode==-1)
			if (cmd.equals(commands[idx])) returnCode = codes[idx];
		return returnCode;
	}

}
